//
//  UIDevice+DPI.m
//  CareRing
//
//  Created  on 2025/3/1.
//
    

#import "UIDevice+DPI.h"
#import <sys/utsname.h>
@implementation UIDevice (DPI)


- (NSString *)deviceModelIdentifier {
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

- (CGFloat)screenDPI {
    NSString *modelIdentifier = [self deviceModelIdentifier];
    
    NSDictionary *deviceDPIMap = @{
        // iPhone 14 系列
        @"iPhone14,4": @460.0,  // iPhone 13 mini
        @"iPhone14,5": @460.0,  // iPhone 13
        @"iPhone14,2": @460.0,  // iPhone 13 Pro
        @"iPhone14,3": @458.0,  // iPhone 13 Pro Max
        @"iPhone14,7": @460.0,  // iPhone 14
        @"iPhone14,8": @460.0,  // iPhone 14 Plus
        @"iPhone15,2": @460.0,  // iPhone 14 Pro
        @"iPhone15,3": @460.0,  // iPhone 14 Pro Max
        
        // iPhone 12/13 系列
        @"iPhone13,1": @476.0,  // iPhone 12 mini
        @"iPhone13,2": @460.0,  // iPhone 12
        @"iPhone13,3": @460.0,  // iPhone 12 Pro
        @"iPhone13,4": @458.0,  // iPhone 12 Pro Max
        
        // iPad Pro 系列
        @"iPad8,1": @264.0,    // iPad Pro 11-inch
        @"iPad8,2": @264.0,    // iPad Pro 11-inch
        @"iPad8,3": @264.0,    // iPad Pro 11-inch
        @"iPad8,4": @264.0     // iPad Pro 11-inch
    };
    
    NSNumber *dpi = [deviceDPIMap objectForKey:modelIdentifier];
    if (dpi) {
        return [dpi floatValue];
    }
    
    // 如果找不到对应的设备，返回默认值
    if ([modelIdentifier hasPrefix:@"iPad"]) {
        return 264.0;  // iPad默认DPI
    }
    return 326.0;  // iPhone默认DPI
}

@end
